//
//  MCBasicViewController+LoadServerData.m
//  MCPublicModule
//
//  Created by Mirko on 16/2/25.
//  Copyright © 2016年 Mirko. All rights reserved.
//

#import "MCBasicViewController+LoadServerData.h"
#import "SVPullToRefresh.h"


@implementation MCBasicViewController(LoadServerData)

/**
 *  下拉刷新
 */
-(void) pullDownTable
{
    self.pageIdex = 0;
    self.mcTableView.showsInfiniteScrolling = YES;
//    [self.mcTableDataSource removeAllObjects];
    [self loadServerData];
}

/**
 *  上拉刷新
 */
-(void) pullUpTable
{
    self.pageIdex += 1;
    self.currentPage = self.pageIdex + self.pageStep;
    [self loadServerData];
}

-(void) loadServerData
{
    
    
}

-(void) setupRefreshTableView
{
    __weak MCBasicViewController *weakSelf = self;
    [self.mcTableView addPullToRefreshWithActionHandler:^{
        [weakSelf pullDownTable];
    }];
    
    [self.mcTableView addInfiniteScrollingWithActionHandler:^{
        [weakSelf pullUpTable];
    }];
}

-(void) loadServerDataComplete:(NSInteger) newDataCount;
{
    dispatch_async(dispatch_get_main_queue(), ^{
        if (self.mcTableView.pullToRefreshView.state == SVPullToRefreshStateLoading) {
            [self.mcTableView.pullToRefreshView stopAnimating];
        }
        
        if (self.mcTableView.infiniteScrollingView.state == SVPullToRefreshStateLoading) {
            [self.mcTableView.infiniteScrollingView stopAnimating];
            if (newDataCount == kDataLoadCompleteStatusNoData) {
                self.mcTableView.showsInfiniteScrolling = NO;
            }
        }
        [self.mcTableView reloadData];
        
        if (newDataCount == kDataLoadCompleteStatusFailed) {
            [self loadServerDataNetworkFaild];
        }
    });
}

-(void) loadServerDataNetworkFaild
{
    self.pageIdex -= 1;
}


@end
